﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;

public partial class Student_ScoreCard : System.Web.UI.Page
{
    private UserControls_ScoreCardControl controlObj = new UserControls_ScoreCardControl();
    ArrayList arrAllQuestionobj = new ArrayList();
    int testid;
    Information ansObj = new Information();
    string attemptId;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString["AttemptId"] == null)
            {
                getValues();
            }
            else
                setValues();
        }
        controlObj.fillQuestionDs(Int32.Parse(Request.QueryString["TestId"].ToString()));
        attemptId = Session["AttemptId"].ToString();
    }

    private void setValues()
    {
        Session["AttemptId"]=Request.QueryString["AttemptId"];
        attemptId=Session["AttemptId"].ToString();
        controlObj = (UserControls_ScoreCardControl)LoadControl("../UserControls/ScoreCardControl.ascx");
        ControlPlace.Controls.Add(controlObj);
        DataTable dtTestAttempt = new DataTable();
        dtTestAttempt = AdminCreateTestDAL.GetTestCompletedAttemptAllInfo(Convert.ToInt32(Request.QueryString["AttemptId"]));
        testid = Convert.ToInt32(dtTestAttempt.Rows[0][2]);
        //userid = dtTestAttempt.Rows[0][1];
        GetHeaderFields();
        controlObj.fillQuestionDs(testid);
        DataTable dtAttempt = new DataTable();
        dtAttempt = AdminCreateTestDAL.GetTestAttemptAllInfo(Session["userId"], testid);
        if (Convert.ToInt32(lablCutOff.Text) > 0)
        {
            lablPassFail.Visible = true;
            lablPassFail.Text = dtAttempt.Rows[0][2].ToString();
            if (lablPassFail.Text == "PASS")
            {
                lablPassFail.ForeColor = System.Drawing.Color.Green;
            }
            else
            {
                lablPassFail.ForeColor = System.Drawing.Color.Red;
            }

        }
        lablTimeTaken.Text = dtTestAttempt.Rows[0][7].ToString();
        lablMarksScored.Text = dtTestAttempt.Rows[0][5].ToString();
        controlObj.pos = 0;
        controlObj.displayQuestion(controlObj.pos,attemptId );
    }

    private void getValues()
    {
        controlObj = (UserControls_ScoreCardControl)LoadControl("~/UserControls/ScoreCardControl.ascx");
        ControlPlace.Controls.Add(controlObj);
        testid = Int32.Parse(Request.QueryString["TestId"].ToString());
        lablTimeTaken.Text = Request.QueryString["TimeTaken"].ToString();
        GetHeaderFields();
        controlObj.fillQuestionDs(testid);
        status(testid, controlObj);
        controlObj.pos = 0;
        string Qid = "", OptionId = "";
        ansObj = (Information)Session["Answers"];
        foreach (var s in ansObj.answer)
        {
            Qid += s.Key + "|";
            OptionId += s.Value + "&";
        }
        DataTable dtTestAttempt = new DataTable();
        dtTestAttempt = AdminCreateTestDAL.GetTestAttemptInfo(Session["UserId"], testid);
        int testAttNo;
        if (dtTestAttempt.Rows.Count > 0)
        {
            testAttNo = Convert.ToInt32(dtTestAttempt.Rows[dtTestAttempt.Rows.Count - 1][0]);
            testAttNo++;
        }
        else
            testAttNo = 1;
        DataTable dtAtt = AdminCreateTestDAL.SetTestAttempt(testid, lablPassFail.Text, Convert.ToInt32(lablMarksScored.Text), testAttNo, Session["UserId"], Qid, OptionId, Convert.ToInt32(lablTimeTaken.Text));
        Session["AttemptId"]=dtAtt.Rows[0][0];
        attemptId=Session["AttemptId"].ToString();
        controlObj.displayQuestion(controlObj.pos,attemptId);

    }

    private void status(int testid, UserControls_ScoreCardControl objDs)
    {
        int count = 0;
        DataTable dtAnswer = new DataTable();
        for (int i = 0; i < objDs.dsQustn.Tables[0].Rows.Count; i++)
        {
            dtAnswer = DALKAT.GetRightOptionId(Int32.Parse(objDs.dsQustn.Tables[0].Rows[i][2].ToString()));
            ansObj =(Information) Session["Answers"];
            foreach (var s in ansObj.answer)
            {
                if (s.Key.ToString() == objDs.dsQustn.Tables[0].Rows[i][2].ToString())
                {
                    if (s.Value.ToString() == dtAnswer.Rows[0][0].ToString())
                        count++;
                }
            }
        }
        if (Convert.ToInt32(lablCutOff.Text) > 0)
        {
            lablPassFail.Visible = true;
            if (count >= Convert.ToInt32(lablCutOff.Text))
            {
                lablPassFail.Text = "PASS";
                lablPassFail.ForeColor = System.Drawing.Color.Green;
                AdminCreateTestDAL.updateTestStatus(testid, Session["UserId"], "Completed");
            }
            else
            {
                lablPassFail.Text = "FAIL";
                lablPassFail.ForeColor = System.Drawing.Color.Red;
                AdminCreateTestDAL.updateTestStatus(testid, Session["UserId"], "Scheduled");
            }
        }
        if (Convert.ToInt32(lablCutOff.Text) == 0)
        {
            AdminCreateTestDAL.updateTestStatus(testid, Session["UserId"], "Completed");
        }
        lablMarksScored.Text = count.ToString();
    }

    private void GetHeaderFields()
    {
        DataTable dtFields;
        dtFields = DALKAT.GetTestInProgressFields(testid);
        if (dtFields.Rows.Count != 0)
        {
            lablTestName.Text = dtFields.Rows[0][1].ToString();
            lablTotalQues.Text = dtFields.Rows[0][2].ToString();
            lablCutOff.Text = dtFields.Rows[0][4].ToString();
        }
    }

    protected void btnNext_Click(object sender, EventArgs e)
    {
        controlObj = (UserControls_ScoreCardControl)LoadControl("../UserControls/ScoreCardControl.ascx");
        controlObj.fillQuestionDs(Int32.Parse(Request.QueryString["TestId"].ToString()));
        controlObj.pos++;
        if (controlObj.pos > controlObj.dsQustn.Tables[0].Rows.Count - 1)
            controlObj.pos = controlObj.dsQustn.Tables[0].Rows.Count - 1;
        controlObj.displayQuestion(controlObj.pos, attemptId);
    }

    protected void btnBack_Click(object sender, EventArgs e)
    {
        controlObj = (UserControls_ScoreCardControl)LoadControl("../UserControls/ScoreCardControl.ascx");
        controlObj.fillQuestionDs(Int32.Parse(Request.QueryString["TestId"].ToString()));
        controlObj.pos--;
        if (controlObj.pos < 0)
            controlObj.pos = 0;
        controlObj.displayQuestion(controlObj.pos, attemptId);
    }

    protected void btnShowAll_Click(object sender, EventArgs e)
    {
        btnNext.Visible = false;
        btnBack.Visible = false;
        btnShowAll.Visible = false;
        btnShowOneByOne.Visible = true;
        controlObj.fillQuestionDs(Int32.Parse(Request.QueryString["TestId"].ToString()));
        UserControls_ScoreCardControl tempObj;
        for (int k = 0; k < controlObj.dsQustn.Tables[0].Rows.Count; k++)
        {
            arrAllQuestionobj.Add(LoadControl("../UserControls/ScoreCardControl.ascx"));
            tempObj = (UserControls_ScoreCardControl)(arrAllQuestionobj[k]);
            ControlPlace.Controls.Add(tempObj);
            tempObj.fillQuestionDs(Int32.Parse(Request.QueryString["TestId"].ToString()));
            tempObj.displayQuestion(k, attemptId);
        }
    }
   
    protected void btnShowOneByOne_Click(object sender, EventArgs e)
    {
        btnNext.Visible = true;
        btnBack.Visible = true;
        btnShowAll.Visible = true;
        btnShowOneByOne.Visible = false;
        controlObj = (UserControls_ScoreCardControl)LoadControl("../UserControls/ScoreCardControl.ascx");
        ControlPlace.Controls.Add(controlObj);
        controlObj.fillQuestionDs(Int32.Parse(Request.QueryString["TestId"].ToString()));
        controlObj.pos = 0;
        controlObj.displayQuestion(controlObj.pos, attemptId);
    }
}